﻿package effects
{
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	
	public class Effect0 extends Effect
	{
		private var url:String = "assets/jpeg0.jpg";
		private var loader:Loader = new Loader();
		private var matrix:Matrix;
		private var myBitmap:BitmapData;

		public function Effect0() 
		{
			var request:URLRequest = new URLRequest(url);
            
			loader.load(request);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, draw);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 
		}
	    public override function add():void
	    {
	    	addEventListener(Event.ENTER_FRAME,render);
	    }
		public override function remove():void
	    {
	    	removeEventListener(Event.ENTER_FRAME,render);
				
	    	matrix.tx=0;
	    	matrix.ty=0;
	    }
		private function draw(event:Event):void 
		{
		    myBitmap = new BitmapData(loader.width, loader.height, false);
		    myBitmap.draw(loader, new Matrix());
		    
		    matrix = new Matrix();
		    matrix.rotate(Math.PI/4);
		    matrix.b=20;
		    matrix.a=90
		    
		    with(graphics){
			    beginBitmapFill(myBitmap, matrix, true,true);
			    drawRect(0, 0,Global._stage.stageWidth,Global._stage.stageHeight);
			    endFill();
	     	}
	     
	     	addEventListener(Event.ENTER_FRAME,render);
	    }
	    private function render(e:Event):void
	    {
			matrix.tx+=8;
	    	matrix.ty+=2;
	    	with(graphics){
	    		clear();
			    beginBitmapFill(myBitmap, matrix, true,true);
			    drawRect(0, 0,Global._stage.stageWidth,Global._stage.stageWidth);
			    endFill();
	     	}
	    }
		private function ioErrorHandler(event:IOErrorEvent):void 
		{
	        ExternalInterface.call("alert","Unable to load image: " + url);
	    }
	}
}